package com.xx.springboot.service.impl;

import com.xx.springboot.Vo.CommentVo;
import com.xx.springboot.controller.AbstractController;
import com.xx.springboot.entity.Comment;
import com.xx.springboot.entity.ResponseBo;
import com.xx.springboot.entity.User;
import com.xx.springboot.mapper.CommentMapper;
import com.xx.springboot.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xx.springboot.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiunique
 * @since 2018-08-06
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * <p>
     *      添加评论
     * </p>
     *
     * @param comment
     * @return 评论状态
     * @Author hcg
     * @Date 2018-11-20 11:26
     * @Param content 评论内容
     */
    @Override
    public ResponseBo addComment(Comment comment) {
        User user = UserUtils.getUser();
        if (user == null)
            return ResponseBo.error(ResponseBo.LOGIN_TIMEOUT_EXCEPTION_CODE, "登录超时, 请从新登陆");

        comment.setUserId(user.getId());
        comment.setStatus(1);

        if(commentMapper.insert(comment) == 1){
            return ResponseBo.ok();
        } else {
            return ResponseBo.error("添加失败, 请稍后再试");
        }
    }


    /**
     * <p>
     * 查询评论列表
     * </p>
     *
     * @param id
     * @return
     * @Author hcg
     * @Date 2018-11-20 14:38
     * @Param id
     */
    @Override
    public ResponseBo list(Integer id) {
        Map<String, Object> map = new HashMap<>();
        List<CommentVo> comments = commentMapper.getCommentList(id);
        return ResponseBo.ok(comments);
    }
}
